﻿using Kaskelot.SP.Core;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kaskelot.SP.Services
{
	public class AuthService
	{
		public enum Permissions
		{
			Read, Write, Admin
		}

		public static bool IsInGroup(string group)
		{
			if (group != Constants.Groups.Administrators && group != Constants.Groups.Readers && group != Constants.Groups.Workers)
				throw new ArgumentException("Okänt gruppnamn.");

			var v = false;
            v = SPContext.Current.Site.RootWeb.SiteGroups[group].ContainsCurrentUser;

			return v;
		}

		public static bool IsInRole(Permissions p)
		{
			switch (p)
			{
				case Permissions.Admin:
					return IsInGroup(Constants.Groups.Administrators);
				case Permissions.Write:
					return IsInGroup(Constants.Groups.Administrators) || IsInGroup(Constants.Groups.Workers);
				case Permissions.Read:
					return IsInGroup(Constants.Groups.Administrators) || IsInGroup(Constants.Groups.Workers) || IsInGroup(Constants.Groups.Readers);
				default:
					return false;
			}
		}
	}
}
